package com.yejiarong.weather.agent.service.jobhandler;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yejiarong.weather.agent.service.IWeatherDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author YeJR
 */
@Slf4j
@Component
public class WeatherDataJobHandler {

    private final IWeatherDataService weatherDataService;

    public WeatherDataJobHandler(IWeatherDataService weatherDataService) {
        this.weatherDataService = weatherDataService;
    }

    /**
     * 同步全国天气
     */
    @XxlJob("syncChinaWeather")
    public void syncChinaWeather() {
        log.info("开始同步全国天气信息");
        weatherDataService.gatherWeatherData();
    }

    /**
     * 同步某个城市的天气
     */
    @XxlJob("syncCityWeatherInChina")
    public void syncCityWeatherInChina() {
        String city = Optional.ofNullable(XxlJobHelper.getJobParam()).orElse("").trim();
        if (StringUtils.isNotBlank(city)) {
            log.info("开始同步{}天气信息", city);
            weatherDataService.gatherWeatherData(city);
        }
    }

    /**
     * 清理过时的未来天气数据
     */
    @XxlJob("cleanOutdatedFutureWeather")
    public void cleanOutdatedFutureWeather() {
        log.info("开始清理过时的未来天气数据");
        weatherDataService.cleanOutdatedFutureWeatherData();
    }
}
